package com.wanxi.dangdang.scheduler;

import com.wanxi.dangdang.model.dto.OrderDelayDto;
import com.wanxi.dangdang.service.OrderService;
import com.wanxi.dangdang.util.ApplicationContextUtil;

import java.util.concurrent.DelayQueue;

/**
 * @Author: 寇文宝
 * @Description: 订单支付定时器
 * @Date: Created in 2021/9/6
 * @Version: 1.0
 */

public class OrderCheckScheduler implements Runnable {
    //延时队列
    private DelayQueue queue;

    // 注入orderService
    OrderService orderService = (OrderService) ApplicationContextUtil.getBean("orderService");

    public OrderCheckScheduler(DelayQueue queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                OrderDelayDto take = (OrderDelayDto) queue.take();
                orderService.autoCancelOrder(take.getId());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
